---@class RedRob.PlayerAutoFightAi : RedRob.PlayerAi
local PlayerAutoFightAi = RedRob.CreateClass("PlayerAutoFightAi","PlayerAi")
local super = RedRob.PlayerAi

function PlayerAutoFightAi:OnCreate()
    super.OnCreate(self)
    self.tempList = {}
    self.delayWaitTime = 3
    self.tempTarget = nil
end
function PlayerAutoFightAi:OnDestroy()
    super.OnDestroy(self)
    self.tempList = nil
    self.tempTarget = nil
end
function PlayerAutoFightAi:Update(dt)
    super.Update(self,dt)
    if self.actor:IsInBattle() then
        return
    end
    if self.actor:IsDead() then
        return
    end
    if not self.actor:HasTarget() then--无目标
        self.delayWaitTime = self.delayWaitTime - dt
        local flag = false
        if self.delayWaitTime <= 0 then
            flag = true
            self.delayWaitTime = 1
        end
        if not flag then return end
        local r = math.random(1,100)
        if r < 33 then
            local vec = Vector3.GetCacheVec():Set(RedRob.ClientBattle.GetLimitRandomPos())
            --self:MoveToDestinationByXYZ(vec.x,vec.y)
            RedRob.BattleMsgHandler.ReqMoveByAi(self.actor:GetId(),vec.x,vec.y)
            Vector3.ReturnToPool(vec)
            self.delayWaitTime = 3
        else
            table.clear(self.tempList)
            local actors = RedRob.ActorManager.GetActorList()
            for k,v in pairs(actors) do
                if v ~= self.actor and not v:IsInBattle() and not v:IsDead() then
                    table.insert(self.tempList,v)
                end
            end
            if #self.tempList == 0 then
                return
            end
            local r = math.random(1,#self.tempList)
            ---@type RedRob.Player
            local target = self.tempList[r]
            self.actor:SetTarget(target)
            RedRob.BattleMsgHandler.ReqMoveByAi(self.actor:GetId(),target:GetPos().x,target:GetPos().y)
            --[[self:PursuitTarget(target,self.actor:GetAttackRadius(),function()
                if not target:IsInBattle() then
                    RedRob.BattleMsgHandler.ReqPkByAi(self.actor:GetId(),target:GetId())
                end
            end)]]
        end
    end
    if self.actor:HasTarget() then
        if self.destinationData.isFinished then
            local target = self:GetTarget()
            local dis = Vector3.SqrDistance(target:GetPos(),self.actor:GetPos())
            if dis <= self.actor:GetAttackRadius() * self.actor:GetAttackRadius() then
                if not target:IsInBattle() then
                    RedRob.BattleMsgHandler.ReqPkByAi(self.actor:GetId(),target:GetId())
                    return
                end
            end
            self.actor:SetTarget(nil)
        end
    end
end
function PlayerAutoFightAi:SendMoveMsg(endX,endY)
    RedRob.BattleMsgHandler.ReqMoveByAi(self.actor:GetId(),endX,endY)
end